package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.dto.MenuDtos;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.dto.RoleInfo;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_role")
@Slf4j
public class Rolecontroller {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResultData roleList(int page, @RequestParam("limit") int pageSize, String name) {
        Page<Role> rolePage = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        //添加查询条件
        lqw.like(StringUtils.isNotEmpty(name), Role::getName, name);
        lqw.orderByDesc(Role::getUpdate_date);
        roleService.page(rolePage, lqw);
        List<Role> records = rolePage.getRecords();
        PageResult pageResult = PageResult.init(rolePage.getTotal(), page, pageSize, records);
        return ResultData.ok("", pageResult);
    }
    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody Role role) {
        //roleService.updateWithMenu(roleInfo);
        roleService.updateById(role);
        return ResultData.ok("修改成功",null);
        //return null;
    }
    //添加
    @PostMapping("/add")
    public ResultData add(@RequestBody RoleInfo roleInfo){
        Role role = new Role();
        role.setName(roleInfo.getName());
        role.setDescription(roleInfo.getDescription());
        roleService.save(role);
        ArrayList<Integer> menuids = roleInfo.getMenuids();
        for (Integer menuid : menuids) {
            RoleMenu RoleMenu = new RoleMenu();
            RoleMenu.setRoleId(role.getId());
            RoleMenu.setMenuId(menuid);
            roleMenuService.save(RoleMenu);
        }
        return ResultData.ok("","");
    }
    //角色列表
    @GetMapping("/allData")
    public ResultData allDate(){
        return ResultData.ok("",roleService.list());
    }

    //删除功能
    @GetMapping("/del")
    public ResultData delete(String id) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,id);
        roleMenuService.remove(queryWrapper);
        roleService.removeById(id);
        return ResultData.ok("","");
    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam String id){
        //传来的是roleId
        Role role = roleService.getById(id);
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(role,roleDto);

        //查到所有的role,根据role查到所有的menu_id
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId, id);
        List<RoleMenu> roleMenulist = roleMenuService.list(queryWrapper);

        List<Integer> menuIdList = roleMenulist.stream().map(s -> {
            Integer menu_id = s.getMenuId();
            return menu_id;
        }).collect(Collectors.toList());
        if(menuIdList.size()==0||menuIdList==null){
            return ResultData.ok("",role);
        }

        //查到pid为0的 menu
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.in(Menu::getId,menuIdList);
        menuLambdaQueryWrapper.eq(Menu::getPid,0);
        List<Menu> list = menuService.list(menuLambdaQueryWrapper);

        List<MenuDtos> menuDtoList = list.stream().map(s -> {
            MenuDtos menuDto = new MenuDtos();
            BeanUtils.copyProperties(s, menuDto);
            return menuDto;
        }).collect(Collectors.toList());

        //封装完毕的menudto
        List<MenuDtos> MenuDto = digui(menuDtoList, menuIdList);
        roleDto.setAuthList(MenuDto);


        return ResultData.ok("", roleDto);

    }

    public List<MenuDtos> digui(List<MenuDtos> dtoList, List<Integer> menuIdList){
        List<MenuDtos> collect = dtoList.stream().map(s -> {
            Integer id = s.getId();
            LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            menuLambdaQueryWrapper.eq(Menu::getPid, id);
            menuLambdaQueryWrapper.in(Menu::getId,menuIdList);
            List<Menu> list = menuService.list(menuLambdaQueryWrapper);
            if (list.size()==0) {
                return s;
            }

            List<MenuDtos> menuDtoList_angin = list.stream().map(s1 -> {
                MenuDtos menuDto = new MenuDtos();
                BeanUtils.copyProperties(s1, menuDto);
                return menuDto;
            }).collect(Collectors.toList());

            s.setChildren(menuDtoList_angin);

            digui(menuDtoList_angin,menuIdList);

            return s;

        }).collect(Collectors.toList());

        return collect;
    }
    }

